package com.genebio.equinox.update;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.eclipse.update.configuration.IConfiguredSite;
import org.eclipse.update.core.IFeatureReference;
import org.eclipse.update.standalone.ListFeaturesCommand;
import org.eclipse.update.standalone.ScriptedCommand;
import org.eclipse.update.standalone.StandaloneUpdateApplication;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

public class Feature extends ScriptedCommand{
	private IConfiguredSite[] sites;
	public Feature() throws Exception {
		super(null);
		sites = getConfiguration().getConfiguredSites();
	}
	
	public boolean list(HttpServletResponse resp) throws IOException {
		try {
			resp.getWriter().println("<pre>");
			if (sites != null) {
				for (int i = 0; i < sites.length; i++) {
					resp.getWriter().println("Site: " + sites[i].getSite().getURL()); //$NON-NLS-1$
					IFeatureReference[] features = sites[i]
							.getFeatureReferences();
					for (int f = 0; f < features.length; f++) {
						boolean configured = sites[i].isConfigured(features[f]
								.getFeature(null));
						resp.getWriter().println("  Feature: " //$NON-NLS-1$
								+ features[f].getVersionedIdentifier()
										.getIdentifier()
								+ " " //$NON-NLS-1$
								+ features[f].getVersionedIdentifier()
										.getVersion() + "  " //$NON-NLS-1$
								+ (configured ? "enabled" : "disabled")); //$NON-NLS-1$ //$NON-NLS-2$
					}
				}
			}
			resp.getWriter().println("</pre>");
			return true;
		} catch (CoreException e) {
			resp.getWriter().println(e.getMessage());
			return false;
		}
	}

	@Override
	public boolean run(IProgressMonitor monitor) {
		// TODO Auto-generated method stub
		return false;
	}	
}
